﻿# -*- coding: utf-8 -*-
import cherrypy
import bd

FORM_CREATE = """<br/>\n<form action="view" method="post">
FIO: <input type="text" name="fio"/><br/>
Phone: <input type="text" name="phone"/><br/>
<input type="submit" value="Искать"/>
</form>\n<br/>\n"""

FORM_FIND = """<br/>\n<form action="create" method="post">
FIO*: <input type="text" name="fio"/><br/>
Phone*: <input type="text" name="phone"/><br/>
Addres*: <input type="text" name="addr"/><br/>
<input type="submit" value="Создать"/>
</form>\n<br/>\n"""

class MainPage:
    Title = u"Моя первая страница на Питоне"

    def __init__(self):
        self.db = bd.mdb("2sprav.csv")

    def index(self):
        cherrypy.session['1'] = 123
        result = """<html><body>\nRecords in DB: %d \n""" % self.db.count()
        result += FORM_FIND
        result += FORM_CREATE
        result += """</body></html>"""

        return result
    index.exposed = True


    def view(self, fio="", phone=""):
        result = """<html><body>\n"""
        result += FORM_FIND
        f_res = self.db.read(fio, phone)
        result += "Found: %d records.<br>" % len(test)
        for rec in f_res:
            result += rec[0]+" : "+rec[1]+" : "+rec[2]+"<br>\n"
        result += """</body></html>"""
        return result
    view.exposed = True


    def create(self, fio="", phone="", addr=""):
        result = """<html><body>\n"""
        result += FORM_CREATE
        f_res = self.db.read(fio, phone, addr)
        result += "Created: %d records.<br>" % len(test)
        for rec in f_res:
            result += rec[0]+" : "+rec[1]+" : "+rec[2]+"<br>\n"
        result += """</body></html>"""
        return result
    create.exposed = True


    def default(self, method):
        if method == 'Khnya':
            out = "Khnya!"
        else:
            out = "Not Khnya!"
        return '%s (<a href="./">back</a>)' % out
    default.exposed = True


if __name__ == "__main__":
    import os.path
    saitconf = os.path.join( os.path.dirname(__file__), "sait.conf" )
    cherrypy.tree.mount( MainPage() )
    cherrypy.quickstart( MainPage(), config=saitconf )
